import clr, re, sys

clr.AddReferenceByPartialName("Microsoft.Office.Interop.Outlook")
from Microsoft.Office.Interop import Outlook

from ExplorerUi import *
from InspectorUi import *
from Globals import *

class OutlookApplication:
    def __init__(self, outlook):
        Outlook.ApplicationEvents_11_Event.add_Quit(outlook, self.OnShutdown)
        
        self.uiList = []
        self.explorers = outlook.Explorers
        self.inspectors = outlook.Inspectors
        
        self.OnStartup()
        
    def OnStartup(self):
        for explorer in self.explorers:
            self.uiList.append(ExplorerUi(explorer, self.uiList, False))

        for inspector in self.inspectors:
            self.uiList.append(InspectorUi(inspector, self.uiList, False))

        self.explorers.NewExplorer += self.OnNewExplorer
        self.inspectors.NewInspector += self.OnNewInspector

    def OnShutdown(self):
        for ui in self.uiList:
            ui.Detach()

    def OnNewExplorer(self, explorer):
        try:
            self.uiList.append(ExplorerUi(explorer, self.uiList, True))
        except:
            ReportException()
        
    def OnNewInspector(self, inspector):
        try:
            self.uiList.append(InspectorUi(inspector, self.uiList, False))
        except:
            ReportException()
